package com.onlineMIS.ORM.DAO.headQ.finance;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Projections;
import org.hibernate.criterion.Restrictions;
import org.springframework.stereotype.Repository;
import com.onlineMIS.ORM.DAO.BaseDAO;
import com.onlineMIS.ORM.entity.chainS.vip.ChainVIPCard;
import com.onlineMIS.ORM.entity.chainS.vip.ChainVIPPrepaidFlow;
import com.onlineMIS.ORM.entity.headQ.finance.ChainAcctFlow;
import com.onlineMIS.ORM.entity.headQ.finance.FinanceCategory;
import com.onlineMIS.ORM.entity.headQ.finance.FinanceCategoryFlow;
import com.onlineMIS.common.Common_util;

@Repository
public class FinanceCategoryFlowImpl extends BaseDAO<FinanceCategoryFlow> {
	/**
	 * get the accumulate acct flow
	 * @return
	 */
	public double getAccumulateAcctFlow(int financeCategoryId){
		DetachedCriteria criteria = DetachedCriteria.forClass(FinanceCategoryFlow.class);

		criteria.add(Restrictions.eq("financeCategoryId", financeCategoryId));
		
		criteria.setProjection(Projections.sum("amt"));
		
		List<Object> result = getByCriteriaProjection(criteria, true);

        return Common_util.getProjectionDoubleValue(result);
	}
	
	/**
	 * get the accumulate acct flow
	 * @return
	 */
	public Map<Integer, Double> getAccumulateAcctFlowMap(){
		Map<Integer, Double> flowMap = new HashMap<Integer, Double>();

			String sql = "select financeCategoryId, sum(amt) from FinanceCategoryFlow where delflag = " +FinanceCategoryFlow.IS_NORMAL+ " group by financeCategoryId";
			
			List<Object> results_accu = this.executeHQLSelect(sql, new Object[]{}, null,true);
			
			if (results_accu != null){
			    for (Object object : results_accu){
			    	Object[] oneRecord = (Object[])object;
			    	int vipCardId = (Integer)oneRecord[0];
			    	Double amount = Common_util.getDouble(oneRecord[1]);
			    	flowMap.put(vipCardId, amount);
			    }
			} 
		
		return flowMap;

	}
}
